﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TheVip
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            HienThi();
            if (!capnhat)
                Them();
        }

        void HienThi()
        {
            hienthi = Common.StaticVariable.InterfaceObject.SelectTheVip("1");
            dgvHienThi.DataSource = hienthi;
            fix();
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.TheVip.GhiChu].Visible = true;
            dgvHienThi.Columns[Common.Constants.TheVip.GiaTriConLai].Visible = true;
            dgvHienThi.Columns[Common.Constants.TheVip.LoaiThe].Visible = true;
            dgvHienThi.Columns[Common.Constants.TheVip.MaKhachHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.TheVip.MaTheVip].Visible = true;
            dgvHienThi.Columns[Common.Constants.TheVip.TenKhachHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.TheVip.GhiChu].HeaderText = "Ghi Chú";
            dgvHienThi.Columns[Common.Constants.TheVip.GiaTriConLai].HeaderText = "Giá Trị Còn Lại";
            dgvHienThi.Columns[Common.Constants.TheVip.LoaiThe].HeaderText = "Loại Thẻ Vip";
            dgvHienThi.Columns[Common.Constants.TheVip.MaKhachHang].HeaderText = "Mã Khách Hàng";
            dgvHienThi.Columns[Common.Constants.TheVip.MaTheVip].HeaderText = "Mã Thẻ Vip";
            dgvHienThi.Columns[Common.Constants.TheVip.TenKhachHang].HeaderText = "Tên Khách Hàng";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Khởi tạo
        bool checkClose = false;
        Entities.TheVip thevip;
        Entities.TheVip[] hienthi;
        int i = 0;
        public frmQuanLy()
        {
            InitializeComponent();
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            return Common.Utilities.Check(Common.FormName.QuanLyTheVip, hanhDong);
        }
        #endregion

        #region Thêm
        private void Them()
        {
            try
            {
                GUI.TheVip.frmXuLy frm = new GUI.TheVip.frmXuLy();
                frm.GetData = new GUI.TheVip.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch
            { }
        }
        #endregion

        #region Sửa
        private void Sua()
        {
            try
            {
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }
                thevip = ((Entities.TheVip[])dgvHienThi.DataSource)[i];
                if (thevip == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                GUI.TheVip.frmXuLy frm = new GUI.TheVip.frmXuLy(thevip);
                frm.GetData = new GUI.TheVip.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Tìm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.TheVip[])hienthi.Where(p => p.MaTheVip.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            fix();
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Event
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            try
            {

                btnThem.Click += new EventHandler(btnThem_Click);
                btnThoat.Click += new EventHandler(btnThoat_Click);
                btnTim.Click += new EventHandler(btnTim_Click);
                txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
                this.FormClosing += new FormClosingEventHandler(frmQuanLy_FormClosing);
                HienThi();
                dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
                dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
                dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown); this.WindowState = FormWindowState.Maximized;
            }
            catch
            {
            }
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Sua();
            }
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Sua();
        }
        
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion